<?php

include_once('global.php');
include_once('session.php');
include_once('dom.php');
include_once('cover.php');
include_once('favorites.php');


//--------------------------------------------------------------------------------
// Validate a photo name
//--------------------------------------------------------------------------------
function validatePhotoName($n) {
  $name = substr($n, 0, 16);
  if( validateSessionID($name) == null ) {
    debug("Invalid photo name $n");
    return null;
  }
  return $n;
}

//--------------------------------------------------------------------------------
// Load all photos for a user
// @param $pseudo : name of the user to which we shoudl retrieve photos
// @return a sorted array of photo names for this user
//--------------------------------------------------------------------------------
function loadPhotos($pseudo) {
  $photos = array();
  debug("Loading photos of $pseudo");
  $query = "SELECT name FROM images WHERE user='$pseudo' ORDER BY cat, ordinal, name";
  debugSQL($query);
  $rs = mysql_query( $query );
  if( $rs > 0 ) {
    while( list($name) = mysql_fetch_row ($rs) ) {
      $photos[] = $name;
      debug("photo: $name");
    }
  }
  return $photos;
}

//--------------------------------------------------------------------------------
// Show the page for a photo
//--------------------------------------------------------------------------------
function showPhotoPage() {
  global $dom;
  global $photo;
  global $pseudo;
  debug("Showing photo page for $photo");
  if( $photo == null || $photo == "" ) {
    addFault("No photo to display");
    return;
  }

  $query = "SELECT i.name, i.cat, i.ordinal, i.candidate, i.description, i.user, u.cover, i.upload_time ";
  $query .= " FROM images i, users u ";
  $query .= " WHERE i.user=u.pseudo AND i.name='$photo'";
  debugSQL($query);
  $rs = mysql_query( $query );
  if( $rs > 0 ) {
    list($name, $cat, $ordinal, $candidate, $description, $user, $cover, $upload_time) = mysql_fetch_row ($rs);
    $xroot = $dom->documentElement;
    $ximage = $dom->createElement("image");
    $updatable = 0;
    if( $user == $pseudo ) $updatable = 1;
    if( $cover == null ) $cover = getFirstCover($user);
    $ximage->setAttribute("name", $name);
    $ximage->setAttribute("cat", $cat);
    $ximage->setAttribute("candidate", $candidate);
    $ximage->setAttribute("description", $description);
    $ximage->setAttribute("updatable", $updatable);
    $ximage->setAttribute("user", $user); // owner of this photo
    $ximage->setAttribute("albumCover", $cover); // cover of this photo's album
    $ximage->setAttribute("upload_time", $upload_time);
    $xroot->appendChild($ximage);

    // Set the favorite attribute if somebody is logged in and this photo is one of its favorites
    if( $pseudo != null && $pseudo != "" ) {
      $favorite = isFavorite($name, $pseudo);
      $ximage->setAttribute("favorite", $favorite);
    }

    // If displaying somebody's albums, then retrieve the photos list to display
    // previous and next photo
    if( $user != null ) {
      $photos = loadPhotos($user);
      $len = sizeof($photos) - 1;
      debug("Looking from previous and next photos (among $len)");
      for( $idx = 0; $idx <= $len; $idx++ ) {
        if( $photo == $photos[$idx] ) {
          if( $idx > 0 ) $ximage->setAttribute("previous", $photos[$idx-1]);
          if( $idx < $len ) $ximage->setAttribute("next", $photos[$idx+1]);
          break;
        }
      }    
    }
  }
}

//--------------------------------------------------------------------------------
// Send the Contestants page
//--------------------------------------------------------------------------------
function showContestantsPage() {
  global $dom;
  debug("Showing contestants page");

  $xroot = $dom->documentElement;
  $xcontestants = $dom->createElement("contestants");

  $query = "SELECT u.pseudo, u.realname, u.email, u.cover ";
  $query .= "FROM users u WHERE u.contestant=1";
  debugSQL($query);
  $rs = mysql_query( $query );
  if( $rs > 0 ) {
    while( list($pseudo, $realname, $email, $cover) = mysql_fetch_row ($rs) ) {
      if( $cover == null ) 
        $cover = getFirstCover($pseudo);
      $xprofile = $dom->createElement("profile");
      $xprofile->setAttribute("pseudo", $pseudo);
      $xprofile->setAttribute("realname", $realname);
      $xprofile->setAttribute("cover", $cover);
      $xcontestants->appendChild($xprofile);
    }
  }

  $xroot->appendChild($xcontestants);
}


//--------------------------------------------------------------------------------
// Show somebody's album
//--------------------------------------------------------------------------------
function showAlbum() {
  global $sessionid;
  global $albumof;
  global $dom;
  debug("Showing album of $albumof");

  $xroot = $dom->documentElement;
  $xalbum = $dom->createElement("album");
  $xalbum->setAttribute("title", "$albumof's Jumping photos");

  $ximages1 = $dom->createElement("images");
  $ximages2 = $dom->createElement("images");
  $ximages3 = $dom->createElement("images");
  $ximages1->setAttribute("cat", "1");
  $ximages2->setAttribute("cat", "2");
  $ximages3->setAttribute("cat", "3");
  $query = "SELECT name, cat, ordinal, candidate, description, upload_time FROM images ";
  $query .= " WHERE user='$albumof' ORDER BY cat, ordinal, name";
  debugSQL($query);
  $rs = mysql_query( $query );
  if( $rs > 0 ) {
    while( list($name, $cat, $ordinal, $candidate, $description, $upload_time) = mysql_fetch_row ($rs) ) {
      $cat = (int)$cat;
      $candidate = (int)$candidate;
      if( $description=="-" ) $description = "";
      $ximage = $dom->createElement("image");
      $ximage->setAttribute("name", $name);
      $ximage->setAttribute("cat", $cat);
      $ximage->setAttribute("candidate", $candidate);
      $ximage->setAttribute("ordinal", $ordinal);
      $ximage->setAttribute("description", $description);
      $ximage->setAttribute("upload_time", $upload_time);
      if( $cat == 1 ) $ximages1->appendChild($ximage);
      if( $cat == 2 ) $ximages2->appendChild($ximage);
      if( $cat == 3 ) $ximages3->appendChild($ximage);
    }
  }
  $xalbum->appendChild($ximages1);  
  $xalbum->appendChild($ximages2);  
  $xalbum->appendChild($ximages3);  

  // Album information
  $query = "SELECT u.pseudo, u.realname, u.email, u.contestant, u.cover ";
  $query .= "FROM users u ";
  $query .= "where u.pseudo='$albumof'";
  debugSQL($query);
  $rs = mysql_query( $query );
  if( $rs > 0 ) 
    list($pseudo, $realname, $email, $contestant, $cover) = mysql_fetch_row ($rs);
  if( $cover == null )
    $cover = getFirstCover($pseudo);
  $xprofile = $dom->createElement("profile");
  $xprofile->setAttribute("pseudo", $pseudo);
  $xprofile->setAttribute("realname", $realname);
  $xprofile->setAttribute("email", $email);
  $xprofile->setAttribute("contestant", $contestant);
  $xprofile->setAttribute("cover", $cover);
  $xalbum->appendChild($xprofile);

  $xroot->appendChild($xalbum);
}

//--------------------------------------------------------------------------------
// Update the photo being watched
//--------------------------------------------------------------------------------
function updatePhoto() {
  global $action;
  global $category;
  global $description;
  global $candidate;
  global $photo;
  global $pseudo;
  $category = (int)$category;
  if( $description == null || $description=="" )
    $description = "-";

  if( !isCurrentUserValidated() ) {
    addFault("You account have been rejected");
    return;
  }  

  debug("Updating photo $photo of user $pseudo : cat=$category candidate=$candidate desc=$description");

  $query = "UPDATE images SET cat=$category, description='$description', candidate=$candidate WHERE name='$photo' AND user='$pseudo'";
  debugSQL($query);
  mysql_query( $query );

  pageSwitchBack("photo");
}



?>
